package com.ui;

import com.service.GroupService;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddGroupDialog extends JDialog {
    private JTextField groupNameField;

    public AddGroupDialog(GroupService groupService, String currentClass) {
        setTitle("添加小组");
        setSize(300, 150);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        groupNameField = new JTextField(20);
        JButton addButton = new JButton("新增");
        JButton backButton = new JButton("返回");

        panel.add(new JLabel("小组名称:"));
        panel.add(groupNameField);
        panel.add(addButton);
        panel.add(backButton);

        add(panel);

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String groupName = groupNameField.getText().trim();
                if (!groupName.isEmpty()) {
                   
                    // 在这里添加添加小组的逻辑
                    groupService.addGroup(groupName, Integer.parseInt(currentClass)); 
                    JOptionPane.showMessageDialog(AddGroupDialog.this, "添加成功");
                    dispose(); // 关闭对话框
                } else {
                    JOptionPane.showMessageDialog(AddGroupDialog.this, "小组名称不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose(); // 关闭对话框
            }
        });
    }
}
